package com.physique.tea.Service.Impl;


import com.physique.tea.Global.Result;
import com.physique.tea.Global.ResultList;
import com.physique.tea.Service.DeepSeekService;
import com.physique.tea.dto.Ai.DeepSeekMessageDto;
import com.physique.tea.dto.Ai.DeepSeekRequestDto;
import com.physique.tea.dto.Ai.DeepSeekResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DeepSeekServiceImpl implements DeepSeekService {
    @Value("${deepseek.key}")
    String key;
    @Value("${deepseek.url}")
    String url;
    @Value("${deepseek.model}")
    String model;

    @Override
    public String call(String message) {

        HttpHeaders headers = getHeaders();
        DeepSeekRequestDto request = new DeepSeekRequestDto();
        request.setModel(model);

        //设置给AI的提示词
        DeepSeekMessageDto prompt = new DeepSeekMessageDto();
        prompt.setContent("你是一名专业老中医,你需要根据我的问题给予对应的茶饮配方，你只能从这些配方里面挑选：");
        prompt.setRole("system");

        //用户消息
        DeepSeekMessageDto userMessage = new DeepSeekMessageDto(message,"user");
        List<DeepSeekMessageDto> list = new ArrayList<>();

        list.add(prompt);
        list.add(userMessage);

        request.setMessages(list);
        HttpEntity<DeepSeekRequestDto> entity =  new HttpEntity<>(request, headers);
        RestTemplate restTemplate = new RestTemplate();
        //发起请求并接受响应
        ResponseEntity<DeepSeekResponseDto> response = restTemplate.exchange(url, HttpMethod.POST, entity, DeepSeekResponseDto.class);
        String content = response.getBody().getChoices().get(0).getMessage().getContent();

        return content;
    }




    public HttpHeaders getHeaders(){
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json");
        headers.add("Authorization", "Bearer "+key);

        return headers;
    }

    @Override
    public Result getQuestionResult(String message){

        HttpHeaders headers = getHeaders();
        DeepSeekRequestDto request = new DeepSeekRequestDto();
        request.setModel(model);

        //设置给AI的提示词
        DeepSeekMessageDto prompt = new DeepSeekMessageDto();
        prompt.setContent("你是一名专业老中医,你需要根据我提供的身体状况和健康诉求给我推荐茶饮,你的回答格式必须是：一个茶名,功效,冲泡方法。尽可能简短");
        prompt.setRole("system");

        DeepSeekMessageDto userMessage = new DeepSeekMessageDto(message,"user");

        List<DeepSeekMessageDto> list =new ArrayList<>();
        list.add(prompt);
        list.add(userMessage);
        request.setMessages(list);

        HttpEntity<DeepSeekRequestDto> entity =  new HttpEntity<>(request, headers);
        RestTemplate restTemplate = new RestTemplate();
        //发起请求并接受响应
        ResponseEntity<DeepSeekResponseDto> response = restTemplate.exchange(url, HttpMethod.POST, entity, DeepSeekResponseDto.class);
        String content = response.getBody().getChoices().get(0).getMessage().getContent();

        return new Result(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage(),
                content);

    }
}
